package com.sixth.controller;

import com.sixth.bean.MarketSystem;
import com.sixth.bean.common.BaseRespVo;
import com.sixth.bean.vo.ConfigExpressVO;
import com.sixth.bean.vo.ConfigMallVO;
import com.sixth.bean.vo.ConfigOrderVO;
import com.sixth.bean.vo.ConfigWXVO;
import com.sixth.other.MarketConfig;
import com.sixth.other.MarketFreight;
import com.sixth.other.MarketOrder;
import com.sixth.other.MarketWX;
import com.sixth.service.AdminConfigService;
import com.sixth.util.ConfigUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.json.JacksonJsonParser;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author ljh
 * @version 1.0
 * @describition:
 * @since 2022/9/7 22:03
 **/

@RestController
@RequestMapping("admin/config")
public class AdminConfigController {

    @Autowired
    AdminConfigService adminConfigService;

    JacksonJsonParser jacksonJsonParser = new JacksonJsonParser();

    @RequiresPermissions(value = {"admin:config:mall"}, logical = Logical.OR)
    @RequestMapping(value = "mall", method = RequestMethod.POST)
    public BaseRespVo mall(@RequestBody Map<String,Object> map) {

        adminConfigService.update(map,MarketConfig.list);
        ConfigMallVO configMall = MarketConfig.getConfigMall(map);

        return BaseRespVo.ok(configMall);
    }

    @RequiresPermissions(value = {"admin:config:mall"}, logical = Logical.OR)
    @RequestMapping(value = "mall", method = RequestMethod.GET)
    public BaseRespVo mall() {

        List<MarketSystem> marketSystems = adminConfigService.select(MarketConfig.list);
        ConfigMallVO configMall = MarketConfig.getConfigMall(marketSystems);

        return BaseRespVo.ok(configMall);
    }


    // admin/config/express
    @RequiresPermissions(value = {"admin:config:express"}, logical = Logical.OR)
    @RequestMapping(value = "express",method = RequestMethod.GET)
    public BaseRespVo express(){

        List<MarketSystem> marketSystems = adminConfigService.select(MarketFreight.list);
        ConfigExpressVO configExpressVO = MarketFreight.getConfig(marketSystems);

        return BaseRespVo.ok(configExpressVO);
    }

    @RequiresPermissions(value = {"admin:config:express"}, logical = Logical.OR)
    @RequestMapping(value = "express",method = RequestMethod.POST)
    public BaseRespVo express(@RequestBody Map<String,Object> map){

        adminConfigService.update(map,MarketFreight.list);

        ConfigExpressVO configExpressVO = MarketFreight.getConfig(map);

        return BaseRespVo.ok(configExpressVO);
    }

    @RequiresPermissions(value = {"admin:config:order"}, logical = Logical.OR)
    @RequestMapping(value = "order",method = RequestMethod.GET)
    public BaseRespVo order(){
        List<MarketSystem> marketSystems = adminConfigService.select(MarketOrder.list);
        ConfigOrderVO configOrderVO = new ConfigOrderVO();
        ConfigUtils.getConfig(marketSystems,configOrderVO);

        return BaseRespVo.ok(configOrderVO);
    }

    @RequiresPermissions(value = {"admin:config:order"}, logical = Logical.OR)
    @RequestMapping(value = "order",method = RequestMethod.POST)
    public BaseRespVo order(@RequestBody Map<String, Object> map){

        adminConfigService.update(map,MarketOrder.list);
        ConfigOrderVO configOrderVO = new ConfigOrderVO();
        ConfigUtils.getConfig(map,configOrderVO);

        return BaseRespVo.ok(configOrderVO);
    }

    //config/wx
    @RequiresPermissions(value = {"admin:config:wx"}, logical = Logical.OR)
    @RequestMapping(value = "wx",method = RequestMethod.GET)
    public BaseRespVo wx(){
        List<MarketSystem> marketSystems = adminConfigService.select(MarketWX.list);
        ConfigWXVO configWXVO = new ConfigWXVO();
        ConfigUtils.getConfig(marketSystems,configWXVO);

        return BaseRespVo.ok(configWXVO);
    }

    @RequiresPermissions(value = {"admin:config:wx"}, logical = Logical.OR)
    @RequestMapping(value = "wx",method = RequestMethod.POST)
    public BaseRespVo wx(@RequestBody Map<String, Object> map){

        ConfigWXVO configWXVO = new ConfigWXVO();
        Boolean bool = null;
        if(map.get(MarketWX.marketWxShare) != null){
            Object o = map.get(MarketWX.marketWxShare);
            map.put(MarketWX.marketWxShare,o.toString());

        }
        adminConfigService.update(map,MarketWX.list);
        ConfigUtils.getConfig(map,configWXVO);

        return BaseRespVo.ok(configWXVO);
    }


}


